home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Specifying a Member Function Address For WNDCLASS Structure
- Date: Fri, 23 Feb 1996 15:28:59 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4gkmen$7ja@news.halcyon.com>
- References: <4ggu6k$5c1@mother.usf.edu>
- NNTP-Posting-Host: blv-pm3-ip26.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Ordinary member functions cannot be callbacks. There is an implicit
- argument to ordinary member functions, the "this" pointer, which
- identifies the actual object instance this member function will
- operate on, and you can't communicate to Windows that it really needs
- to call GameControl::WndProc( &aGameControl, hWnd, uMsg,...).
-
- The solution is to make WndProc a *static* method in the GameControl
- class. Static member functions can be callbacks because they don't
- use the implicit "this" pointer.
-
- Now your concern is sticking the GameControl instance in the window,
- which you do by SetWindowLong( GWL_USERDATA, (LPARAM) this ); usually
- within the WM_CREATE handling of your wndproc.
-
- Inside your GameControl::WndProc, you can get at the other GameControl
- methods and members by This = (GameControl *) GetWindowLong(
- GWL_USERDATA ). Then make your calls like
-
- This->AnotherGameControlMethod( x, y, z )
-
- Lastly, you can either instance GameControl in the WM_CREATE handler
- or instance it before the CreateWindow() API and pass the pointer in
- its LPARAM argument.
-
- This approach works very well; I've production code in the field using
- it.
-
- Hope this helps.
-
- --Norm
-
- yatesc@csee.usf.edu (Randy Yates) wrote:
-
- >Hello,
-
- >I'm writing a MS-Windows 3.1 program and I'd like to have the
- >message processing procedure for the window class be a member
- >function...
-
- >class GameControl
- >{
- > private:
- > HWND hWnd;
- > public:
- > GameControl(HINSTANCE, int);
- > long FAR PASCAL _export WndProc(HWND, UINT, UINT, long);
- >};
-
- >GameControl::GameControl(HINSTANCE hInstance, int nCmdShow)
- >{
- >// static char szAppName[] = "GameControl";
- > WNDCLASS wndclass;
-
- > // Create the window:
- > wndclass.style = CS_HREDRAW | CS_VREDRAW;
- > wndclass.lpfnWndProc = &GameControl::WndProc;
- > ...
-
- > hWnd = CreateWindow(
- > PINBULL_CLASSNAME,
- > "PinBull Wizard Game Control",
- > WS_OVERLAPPEDWINDOW,
- > CW_USEDEFAULT,
- > CW_USEDEFAULT,
- > CW_USEDEFAULT,
- > CW_USEDEFAULT,
- > HWND_DESKTOP,
- > NULL,
- > hInstance,
- > NULL);
- >
- > ...
-
- >--
- >% Randy Yates % "...the answer lies within your soul
- >% EE/Mathematics Student % 'cause no one knows which side
- >% University of South Florida % the coin will fall."
- >% <yatesc@csee.usf.edu> % 'Big Wheels', *Out of the Blue*, ELO
-
-
-
-